module ApplicationHelper
  def flash_div *keys
    keys.collect { |key| content_tag(:div, flash[key], :class => "flash #{key}") if flash[key] }.join
  end
  
  def logged_in?
    if current_user != :false
      return true
    end
  end
  
  def format_text(text)
    white_list(simple_format(auto_link(bbcodeize(h(text)))))
  end
  
  def clean_text(text)
    white_list(text)
  end
  
  def has_not_rated_comment? (comment)
    has_rated = 0
    for rating in comment.comment_rating_logs
      if rating[:user_id] == current_user.id
        has_rated = 1
      end
    end
    if has_rated == 1
      return false
    end
    return true
  end

  def has_not_rated_media? (media)
    has_rated = 0
    for rating in media.media_rating_logs
      if rating[:user_id] == current_user.id
        has_rated = 1
      end
    end
    if has_rated == 1
      return false
    end
    return true
  end
  
  def link_to_media_lightbox(media, thumbnail, url_options = {}, html_options = {})
    link_to(image_tag(thumbnail, :style => 'border: 0;'), media, :onclick => "return hs.expand(this, { align: 'center' } )", :class => 'highslide', :onmouseover => 'cancelLink=true', :onmouseout => 'cancelLink=false')
  end
  
  def link_to_component_lightbox(name, url_options = {}, html_options = {})
    id = id_from_url(url_options, html_options[:id])
    lightboxhtml_top = '<div class="highslide-header"><ul><li class="highslide-move"><a href="#" onclick="return false">Move</a></li><li class="highslide-close"><a href="#" onclick="return hs.close(this)">Close</a></li></ul></div><div class="highslide-body">'
    lightboxhtml_bottom = '</div><div class="highslide-footer"><div><span class="highslide-resize" title="Resize"><span></span></span></div></div>'
    link_to_lightbox(name, id, html_options) + content_tag("div", lightboxhtml_top + render_component(url_options) + lightboxhtml_bottom, :id => id, :class => 'highslide-html-content')
  end
  
  def link_to_partial_lightbox(name, url_options = {}, html_options = {})
    id = id_from_url(url_options, html_options[:id])
    lightboxhtml_top = '<div class="highslide-header"><ul><li class="highslide-move"><a href="#" onclick="return false">Move</a></li><li class="highslide-close"><a href="#" onclick="return hs.close(this)">Close</a></li></ul></div><div class="highslide-body">'
    lightboxhtml_bottom = '</div><div class="highslide-footer"><div><span class="highslide-resize" title="Resize"><span></span></span></div></div>'
    link_to_lightbox(name, id, html_options) + content_tag("div", lightboxhtml_top + (render url_options) + lightboxhtml_bottom, :id => id, :class => 'highslide-html-content')
  end
  
  def link_to_lightbox(name, id, html_options = {})
    html_options = html_options.merge!({ :class => 'highslide', :onmouseover => 'cancelLink=true', :onmouseout => 'cancelLink=false' })
    link_to_function name, "return hs.htmlExpand(this, { contentId: '#{id.to_s}', align: 'center', outlineWhileAnimating: true } )", html_options
  end
  
  def id_from_url(url_options, link_id)
    result = ''
    result = link_id.to_s + '_' unless link_id.nil?
    
    if url_options.is_a? String
      result + url_options.delete(":/")
    else
      result + url_options.values.join('_')
    end
  end
end
